package com.COMP3111H.project;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class SellLend_New_Book extends Activity {
	final int HELP = 0;
	Button menu;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.selllend_new_book);
        menu = (Button) findViewById(R.id.SellLend_New_menu);
        
        final EditText newtitletext = (EditText) findViewById(R.id.newtitletext);
        final EditText newcoursestext = (EditText) findViewById(R.id.newcoursestext);
        final EditText newauthorstext = (EditText) findViewById(R.id.newauthorstext);
        final EditText neweditiontext = (EditText) findViewById(R.id.neweditiontext);
        final EditText newisbntext = (EditText) findViewById(R.id.newisbntext);
        final EditText newpricetext = (EditText) findViewById(R.id.sl_new_price_edit);
        final EditText newpublishertext = (EditText) findViewById(R.id.sl_new_publisher_edit);
        
        final Spinner c_spinner = (Spinner) findViewById(R.id.condition_spinner);
        ArrayAdapter<CharSequence> c_adapter = ArrayAdapter.createFromResource(this, R.array.condition_array, android.R.layout.simple_spinner_item);
        c_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        c_spinner.setAdapter(c_adapter);
        c_spinner.setSelection(1);
        
        final Spinner sl_spinner = (Spinner) findViewById(R.id.selllend_spinner);
        ArrayAdapter<CharSequence> sl_adapter = ArrayAdapter.createFromResource(this, R.array.selllend_array, android.R.layout.simple_spinner_item);
        sl_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sl_spinner.setAdapter(sl_adapter);
        sl_spinner.setSelection(0);
        sl_spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
        

		Button cancel = (Button) findViewById(R.id.sl_new_cancel);
		Button cont = (Button) findViewById(R.id.sl_new_cont);
		cont.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if(!LoginUtility.checkLoginStatus(SellLend_New_Book.this)){
					Intent i = new Intent(SellLend_New_Book.this,
							Login.class);
					startActivity(i);
					return;
				}
				
				Intent myIntent = new Intent(SellLend_New_Book.this,
						Agreement.class);
				String title = newtitletext.getText().toString();
				String courses = newcoursestext.getText().toString();
				String authors = newauthorstext.getText().toString();
				String edition = neweditiontext.getText().toString();
				String isbn = newisbntext.getText().toString();		
				String publishers = newpublishertext.getText().toString();
				if(title.matches("") ||  edition.matches("") || isbn.matches("") ){
					Toast.makeText(SellLend_New_Book.this,"Book title, edition and isbn are required.",Toast.LENGTH_LONG).show();
				}
				else{
					if(!NetworkUtility.inputCheck(title) || !NetworkUtility.inputCheck(courses) || 
						!NetworkUtility.inputCheck(authors) || !NetworkUtility.inputCheck(edition) || 
						!NetworkUtility.inputCheck(isbn) || !NetworkUtility.inputCheck(publishers))
						Toast.makeText(SellLend_New_Book.this,"There are special characters that are not allowed in your request", Toast.LENGTH_SHORT).show();
					else{
						myIntent.putExtra("title", title);
						myIntent.putExtra("courses", courses);
						myIntent.putExtra("authors", authors);
						myIntent.putExtra("edition", edition);
						myIntent.putExtra("isbn", isbn);
						myIntent.putExtra("condition", c_spinner.getItemAtPosition(c_spinner.getSelectedItemPosition()).toString());
						myIntent.putExtra("publisher", publishers);
						
						if(sl_spinner.getSelectedItemPosition() == 0){
							String price = newpricetext.getText().toString();
							if(price.matches("")){
								Toast.makeText(SellLend_New_Book.this,"Please input price",Toast.LENGTH_LONG).show();
								return;
							}
							myIntent.putExtra("selllend", "sell");
							myIntent.putExtra("price", price);
							myIntent.putExtra("origin",3);
						}
						else if(sl_spinner.getSelectedItemPosition() == 1){
							myIntent.putExtra("selllend", "lend");
							myIntent.putExtra("origin",3);
						}
						startActivity(myIntent);
					}
				}
			}
		});
		cancel.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				finish();
			}
		});

		Button more_info_c = (Button) findViewById(R.id.more_info_condition);
		Button more_info_sl = (Button) findViewById(R.id.more_info_sl);
		more_info_c.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				Intent myIntent = new Intent(SellLend_New_Book.this, More_InfoBook_Condition.class);
				startActivity(myIntent);
			}
		});

		more_info_sl.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				Intent myIntent = new Intent(SellLend_New_Book.this, More_InfoCredit.class);
				startActivity(myIntent);
			}
		});

	}

	// testing spinner
	public class MyOnItemSelectedListener implements OnItemSelectedListener {
		TextView price = (TextView) findViewById(R.id.sl_new_price);
		EditText price_edit = (EditText) findViewById(R.id.sl_new_price_edit);

		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
//			Toast.makeText(parent.getContext(),
//					parent.getItemAtPosition(pos).toString() + " is selected",
//					Toast.LENGTH_LONG).show();
			if (pos == 1) {
				price.setVisibility(View.GONE);
				price_edit.setVisibility(View.GONE);
			}
			if (pos == 0) {
				price.setVisibility(View.VISIBLE);
				price_edit.setVisibility(View.VISIBLE);
			}
		}

		public void onNothingSelected(AdapterView<?> parent) { // Do nothing.
		}
	}
	
	@Override
    protected void onResume() {
		super.onResume();
		if (LoginUtility.checkLoginStatus(this)) {
			menu.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					 String[] choices={"Home","View My Profile","Help","Cancel"};  
					             AlertDialog dialog = new AlertDialog.Builder(SellLend_New_Book.this)  
					                     // .setIcon(android.R.drawable.btn_star)  
					                      .setTitle("Menu")  
					                      .setItems(choices, onselect_logged).create();  
					             dialog.show();  
				}
			});
		}
		else{
			menu.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					 String[] choices={"Home","Help","Cancel"};  
					             AlertDialog dialog = new AlertDialog.Builder(SellLend_New_Book.this)  
					                     // .setIcon(android.R.drawable.btn_star)  
					                      .setTitle("Menu")  
					                      .setItems(choices, onselect_not_logged).create();  
					             dialog.show();  
				}
			});
		}
	}

	OnClickListener onselect_logged = new OnClickListener() {
		public void onClick(DialogInterface dialog, int which) {
			// TODO Auto-generated method stub
			Intent myIntent;
			switch (which) {
			case 0:
				myIntent = new Intent(SellLend_New_Book.this, Home.class);
				startActivity(myIntent);
				break;
			case 1:
				myIntent = new Intent(SellLend_New_Book.this, My_Profile.class);
				startActivity(myIntent);
				break;
			case 2:
				myIntent = new Intent(SellLend_New_Book.this, Help.class);
				startActivity(myIntent);
				break;
			case 3:
				dialog.dismiss();
				break;
			}
		}
	};
	
	OnClickListener onselect_not_logged = new OnClickListener() {
		public void onClick(DialogInterface dialog, int which) {
			// TODO Auto-generated method stub
			Intent myIntent;
			switch (which) {
			case 0:
				myIntent = new Intent(SellLend_New_Book.this, Home.class);
				startActivity(myIntent);
				break;
			case 1:
				myIntent = new Intent(SellLend_New_Book.this, Help.class);
				startActivity(myIntent);
				break;
			case 2:
				dialog.dismiss();
				break;
			}
		}
	};
}